package com.runachina.common;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;

import com.runachina.R;
import com.runachina.java.TcpSocket;
import com.runachina.othercompany.EuropeCompanyActivity;
import com.runachina.othercompany.OtherCompanyActivity;
import com.runachina.runa.CheckActivity;
import com.runachina.runa.FlowActivity;
import com.runachina.runa.ModifyNumActivity;
import com.runachina.runa.ModifyTimeActivity;
import com.runachina.runa.ReadMeterActivity;
import com.runachina.runa.SetAttributeActivity;
import com.runachina.runa.SetDataActivity;
import com.runachina.runamodbus.RunaModusActivity;
import com.runachina.runamodbus.RunaModusNumActivity;
import com.runachina.yilinvalve.YiLinValveReadMeterActivity;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;


public class BaseActivity extends ActionBarActivity {
    private final String TAG = this.getClass().getSimpleName();

    private Toolbar mToolbar;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;

    public static SharedPreferences localCfg;

    public Intent intent;

    private final Boolean HIDESEND = true;//是否隐藏发送报文

    //public TcpSocket tcpSocket;

    /*下面4行很重要，作为全局判断*/
    public static final String[] READMETHOD = {"mbus", "红外", "485"};
    /*下面文字不要轻易改动，在OtherCompanyActivity和CompanyActivity里面有判断*/
//    public static final String[] COMPANYNAMEARR = {
//            "瑞纳--抄表(188)", "瑞纳--抄表(modbus)", "瑞纳--阀门", "震宇--抄表", "胜利--抄表", "力创--抄表",
//            "兰吉尔--抄表", "汇中--抄表", "伟岸--抄表", "华仪乐--抄表", "迈拓--抄表", "天罡--抄表","亿林--阀门"};
    public static final String[] COMPANYNAMEARR = {"瑞纳--抄表(188)", "瑞纳--抄表(modbus)", "瑞纳--阀门", "亿林--阀门", "瑞纳--温度采集器"};
    public static String companyName = COMPANYNAMEARR[0];
    public static String readMethod = READMETHOD[0];
    /*important used to judge in which activity*/
    public static String inWhichAc = "ReadMeterActivity";

    //判断是否进行版本检测的标志
    public static boolean isQueryAppversion = true;

    /*读取runa 188协议的热表*/
    public static byte[] readmeter = {(byte) 0xfe, (byte) 0xfe, (byte) 0xfe, 0x68, 0x20,
            (byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0xAA,//表号
            (byte) 0xAA, (byte) 0xAA, (byte) 0xAA, 0x01, 0x03, (byte) 0x1F, (byte) 0x90, 0x00,
            (byte) 0x00, 0x16,
            (byte) 0x00, 0x16//存放转发协议的校验和结束
    };

    private static Timer timer = null;    /*创建一个定时器*/
    private static TimerTask task = null; /*timer的使用一定配合着TimerTask*/
    public final static int TI = 7;
    private WifiManager wifiManager;

    /**
     * 隐藏发送报文
     */
    public void hideSendText(TextView send1, TextView send2) {
        if (HIDESEND) {
            send1.setVisibility(View.INVISIBLE);
            send2.setVisibility(View.GONE);
        }
    }

    public void hideSendText(TextView send1, TextView send2, String GONE) {
        if (HIDESEND) {
            send1.setVisibility(View.GONE);
            send2.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("liyuelong", this.getClass().getSimpleName() + ":倒计时onCreate");
      //  tcpSocket = new TcpSocket();
        localCfg = getSharedPreferences("config", MODE_PRIVATE);
    }


    /**
     * getSysData
     */
    public static String getData() {
        SimpleDateFormat a = new SimpleDateFormat("yyyyMMdd");
        String nowData = a.format(new Date());

        return nowData;
    }

    /**
     * getSysTime
     */
    public static String getTime() {
        SimpleDateFormat a = new SimpleDateFormat("hhmmss");
        String nowTime = a.format(new Date());

        return nowTime;
    }

    /**
     * Sleep 毫秒
     */
    public static void sleep(int ms) {
        try {
            Thread.currentThread().sleep(ms);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * byte[] to String,have" "
     */
    public static String byteToHexString(byte[] abyte) {
        String sendString = "", sendtrans = "";
        for (int i = 0; i < abyte.length; i++) {
            sendtrans = Integer.toHexString(abyte[i] & 0xFF).toUpperCase() + "";
            if (sendtrans.length() == 1) sendtrans = "0" + sendtrans;
            sendString = sendString + sendtrans + " ";
        }
        Log.e("lyl", "byteToHexString发送: " + sendString);
        return sendString;
    }

    /**
     * byte[] to String,have" ",from p start to start+L
     */
    public static String byteToHexString(byte[] abyte, int p) {

        int L = 0;
        for (int s = abyte.length - 1; s > 3; s--) {
            if (abyte[s] == (byte) 0x16) {
                L = s - 1;
                if (abyte[s - 2] != 0x16) {
                    L = L + 2;//返回只有1个0x16的时候
                }
                s = 0;//结束循环
            }
        }

        String sendString = "", sendtrans = "";
        for (int i = p; i < L; i++) {
            sendtrans = Integer.toHexString(abyte[i] & 0xFF).toUpperCase() + "";
            if (sendtrans.length() == 1) sendtrans = "0" + sendtrans;
            sendString = sendString + sendtrans + " ";
        }
        Log.e("lyl", "byteToHexString: " + sendString);
        return sendString;
    }

    /**
     * byte[] to String,no" "
     */
    public static String byteToHexString(byte[] abyte, String no) {
        String sendString = "", sendtrans = "";
        for (int i = 0; i < abyte.length; i++) {
            sendtrans = Integer.toHexString(abyte[i] & 0xFF).toUpperCase() + "";
            if (sendtrans.length() == 1) sendtrans = "0" + sendtrans;
            sendString = sendString + sendtrans;
        }
        return sendString;
    }

    //保存本地配置
    static void writeLocalCfg(String name, String msg) {
        Editor edit = localCfg.edit();
        edit.putString(name, msg);
        edit.commit();
    }

    //保存本地配置
    static void writeLocalCfg(String name, int msg) {
        Editor edit = localCfg.edit();
        edit.putInt(name, msg);
        edit.commit();
    }

    //读取本地配置
    String readLocalCfg(String name) {
        return localCfg.getString(name, "");
    }

    int readLocalCfgInt(String name) {
        return localCfg.getInt(name, 0);
    }

    //删除本地配置
    void deleteLocalCfg(String name) {
        Editor edit = localCfg.edit();
        edit.remove(name);
        edit.commit();
    }

    /**
     * 设置toolbar:包括主title,副title，打开左侧侧滑菜单
     */
    public void set_toolbar(String title, String subtitle) {
        //初始化toolbar
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setTitle(title);
        mToolbar.setSubtitle(subtitle);
        mToolbar.setTitleTextColor(Color.WHITE);
        mToolbar.setSubtitleTextColor(Color.WHITE);
        if (mToolbar != null) {
            setSupportActionBar(mToolbar);
        }
        // 打开左侧菜单选项
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.drawer_open, R.string.drawer_close) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                closeBoard();
            }
        };

        //mDrawerToggle.setDrawerIndicatorEnabled(false) ;
        //mToolbar.setNavigationIcon(R.drawable.wifi_connect);

        mDrawerToggle.syncState();
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }

    /**
     * 初始化左侧菜单
     */
    public void set_leftmenu(final Context ac) {
        View.OnClickListener listener = new View.OnClickListener() {
            public void onClick(View v) {
                int id = v.getId();
                switch (id) {
                    case R.id.leftMenu1:
                        startActivity(new Intent(ac, ReadMeterActivity.class));//读取抄表
                        finish();
                        break;
                    case R.id.leftMenu2:
                        startActivity(new Intent(ac, ModifyNumActivity.class));//修改表号
                        finish();
                        break;
                    case R.id.leftMenu3:
                        startActivity(new Intent(ac, ModifyTimeActivity.class));//修改时间
                        finish();
                        break;
                    case R.id.leftMenu4:
                        startActivity(new Intent(ac, SetAttributeActivity.class));//设置仪表属性
                        finish();
                        break;
                    case R.id.leftMenu5:
                        startActivity(new Intent(ac, SetDataActivity.class));//设置仪表数据
                        finish();
                        break;
                    case R.id.leftMenu6:
                        startActivity(new Intent(ac, FlowActivity.class));//流量补偿
                        finish();
                        break;
                    case R.id.leftMenu7:
                        startActivity(new Intent(ac, CheckActivity.class));//检定状态
                        finish();
                        break;
                    case R.id.leftMenu8:
                        startActivity(new Intent(ac, RudeActivity.class));//检定状态
                        finish();
                        break;
                    default:
                        break;
                }
            }
        };
        TextView leftMenu1 = (TextView) findViewById(R.id.leftMenu1);
        leftMenu1.setOnClickListener(listener);
        TextView leftMenu2 = (TextView) findViewById(R.id.leftMenu2);
        leftMenu2.setOnClickListener(listener);
        TextView leftMenu3 = (TextView) findViewById(R.id.leftMenu3);
        leftMenu3.setOnClickListener(listener);
        TextView leftMenu4 = (TextView) findViewById(R.id.leftMenu4);
        leftMenu4.setOnClickListener(listener);
        TextView leftMenu5 = (TextView) findViewById(R.id.leftMenu5);
        leftMenu5.setOnClickListener(listener);
        TextView leftMenu6 = (TextView) findViewById(R.id.leftMenu6);
        leftMenu6.setOnClickListener(listener);
        TextView leftMenu7 = (TextView) findViewById(R.id.leftMenu7);
        leftMenu7.setOnClickListener(listener);
        TextView leftMenu8 = (TextView) findViewById(R.id.leftMenu8);
        leftMenu8.setOnClickListener(listener);
    }


    /**
     * 初始化左侧菜单
     * 瑞纳阀门
     */
    public void set_leftmenu(final Context ac, int type) {
        View.OnClickListener listener = new View.OnClickListener() {
            public void onClick(View v) {
                int id = v.getId();
                Intent intent = new Intent();
                switch (id) {
                    case R.id.leftMenu1:

                        startActivity(new Intent(ac, com.runachina.runavalve.ReadMeterActivity.class));//读取抄表
                        finish();
                        break;
                    case R.id.leftMenu2:

                        startActivity(new Intent(ac, com.runachina.runavalve.ModifyNumActivity.class));//修改表号
                        finish();
                        break;
                    case R.id.leftMenu3:
                        startActivity(new Intent(ac, com.runachina.runavalve.FactoryResetActivity.class));//恢复出厂
                        finish();
                        break;
                    case R.id.leftMenu4:
                        startActivity(new Intent(ac, com.runachina.runavalve.DataResetAvtivity.class));//数据清零
                        finish();
                        break;
                    case R.id.leftMenu5:
                        startActivity(new Intent(ac, com.runachina.runavalve.HardwareResetActivity.class));//硬件复位
                        finish();
                        break;
                    default:
                        break;
                }
            }
        };
        TextView leftMenu1 = (TextView) findViewById(R.id.leftMenu1);
        leftMenu1.setOnClickListener(listener);
        TextView leftMenu2 = (TextView) findViewById(R.id.leftMenu2);
        leftMenu2.setOnClickListener(listener);
        TextView leftMenu3 = (TextView) findViewById(R.id.leftMenu3);
        leftMenu3.setOnClickListener(listener);
        TextView leftMenu4 = (TextView) findViewById(R.id.leftMenu4);
        leftMenu4.setOnClickListener(listener);
        TextView leftMenu5 = (TextView) findViewById(R.id.leftMenu5);
        leftMenu5.setOnClickListener(listener);
    }


    /**
     * 初始化左侧菜单
     * 瑞纳温度采集器
     */
    public void set_leftmenuTemColl(final Context ac) {
        View.OnClickListener listener = new View.OnClickListener() {
            public void onClick(View v) {
                int id = v.getId();
                switch (id) {
                    case R.id.leftMenu1:
                        startActivity(new Intent(ac, com.runachina.temcollector.TemReadMeterActivity.class));//读取抄表
                        finish();
                        break;
                    case R.id.leftMenu2:

                        startActivity(new Intent(ac, com.runachina.temcollector.TemModifyNumActivity.class));//恢复出厂
                        finish();
                        break;
                    case R.id.leftMenu3:

                        startActivity(new Intent(ac, com.runachina.temcollector.TemFactoryResetActivity.class));//恢复出厂
                        finish();
                        break;
                    case R.id.leftMenu4:
                        startActivity(new Intent(ac, com.runachina.temcollector.TemDataResetActivity.class));//数据清零
                        finish();
                        break;
                    case R.id.leftMenu5:
                        startActivity(new Intent(ac, com.runachina.temcollector.TemHardwareResetActivity.class));//硬件复位
                        finish();
                        break;
                    default:
                        break;
                }
            }
        };
        TextView leftMenu1 = (TextView) findViewById(R.id.leftMenu1);
        leftMenu1.setOnClickListener(listener);
        TextView leftMenu2 = (TextView) findViewById(R.id.leftMenu2);
        leftMenu2.setOnClickListener(listener);
        TextView leftMenu3 = (TextView) findViewById(R.id.leftMenu3);
        leftMenu3.setOnClickListener(listener);
        TextView leftMenu4 = (TextView) findViewById(R.id.leftMenu4);
        leftMenu4.setOnClickListener(listener);
        TextView leftMenu5 = (TextView) findViewById(R.id.leftMenu5);
        leftMenu5.setOnClickListener(listener);
    }


    /**
     * 初始化左侧菜单
     * 后面一个参数无效
     */
    public static Class<?> thisca = RudeSimpleActivity.class;

    public void set_leftmenu(final Context ac, final Class<?> ca) {
        View.OnClickListener listener = new View.OnClickListener() {
            public void onClick(View v) {
                if (ca != RudeSimpleActivity.class) {
                    thisca = ca;//记录当前的抄表Class
                }
                int id = v.getId();
                switch (id) {
                    case R.id.leftMenu1:
                        startActivity(new Intent(ac, thisca));//读取抄表
                        finish();
                        break;
                    case R.id.leftMenu8:
                        startActivity(new Intent(ac, RudeSimpleActivity.class));//调试工具
                        finish();
                        break;
                    default:
                        break;
                }
            }
        };
        TextView leftMenu1 = (TextView) findViewById(R.id.leftMenu1);
        leftMenu1.setOnClickListener(listener);
        TextView leftMenu8 = (TextView) findViewById(R.id.leftMenu8);
        leftMenu8.setOnClickListener(listener);
    }


    /**
     * 初始化左侧菜单
     * 后面一个参数无效
     * 亿林温控阀门
     */

    public void set_leftmenuYiLin(final Context ac) {
        View.OnClickListener listener = new View.OnClickListener() {
            public void onClick(View v) {

                int id = v.getId();
                switch (id) {
                    case R.id.leftMenu1:
                        startActivity(new Intent(ac, YiLinValveReadMeterActivity.class));//读取抄表
                        finish();
                        break;

                    default:
                        break;
                }
            }
        };
        TextView leftMenu1 = (TextView) findViewById(R.id.leftMenu1);
        leftMenu1.setOnClickListener(listener);

    }

    /**
     * 初始化左侧菜单
     * 后面一个参数无效
     */

    public void set_leftmenu3(final Context ac) {
        View.OnClickListener listener = new View.OnClickListener() {
            public void onClick(View v) {

                int id = v.getId();
                switch (id) {
                    case R.id.leftMenu1:
                        startActivity(new Intent(ac, RunaModusActivity.class));//读取抄表
                        finish();
                        break;
                    case R.id.leftMenu2:
                        startActivity(new Intent(ac, RunaModusNumActivity.class));//修改表号
                        finish();
                        break;
                    case R.id.leftMenu8:
                        startActivity(new Intent(ac, RudeRunaModbusActivity.class));//调试工具
                        finish();
                        break;
                    default:
                        break;
                }
            }
        };
        TextView leftMenu1 = (TextView) findViewById(R.id.leftMenu1);
        leftMenu1.setOnClickListener(listener);
        TextView leftMenu2 = (TextView) findViewById(R.id.leftMenu2);
        leftMenu2.setOnClickListener(listener);
        TextView leftMenu8 = (TextView) findViewById(R.id.leftMenu8);
        leftMenu8.setOnClickListener(listener);
    }

    /**
     * 隐藏系统键盘
     */
    public void closeBoard() {
        InputMethodManager imm = (InputMethodManager) getApplication().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getCurrentFocus().getApplicationWindowToken(), 0);
    }

    public void show(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }


    /**
     * @return false 已经到期
     * long between_days = (time2 - time1) / (1000 * 3600 * 24);
     */
    public static boolean getDaysBetween(String endTime) {
        SimpleDateFormat data = new SimpleDateFormat("yyyyMMdd");
        String systime = data.format(new Date());//获得手机当前时间
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(data.parse(endTime));
            long endT = cal.getTimeInMillis();

            cal.setTime(data.parse(systime));
            long sysT = cal.getTimeInMillis();

            if (sysT < endT)
                return true;//没有到期
            else
                return false;
        } catch (Exception e) {
            return false;
        }
    }

    //退出系统计时
    private long exitTime = 0;

    /**
     * 退出APP
     */
    public void exitApp() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            companyName = COMPANYNAMEARR[0];//"瑞纳--抄表188";
            if (TcpSocket.mSocketClient != null) {
                try {
                    Log.e("liyuelong1", "TcpSocket.mSocketClient销毁了 ");
                    TcpSocket.mSocketClient.close();
                    TcpSocket.mSocketClient = null;
                    TcpSocket.isConnecting = false;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (timer != null) {
                Log.e("liyuelong1", "exitApp:timer销毁了 ");
                stopTimer();
            }
            if (TcpSocket.handler != null && TcpSocket.runnable != null) {
                Log.e("liyuelong1", "心跳结束 ");
                TcpSocket.handler.removeCallbacks(TcpSocket.runnable);
            }
            TcpSocket.closeHeart();
            isQueryAppversion = true;
            finish();
        }
    }


    public static int count = 6;

    //定时器响应时间
    static android.os.Handler mHandler = new android.os.Handler() {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            startTimer(--count);
            String str = "" + "..." + count;
            if (count <= 0) {
                stopTimer();
                str = "无返回,请检查后再操作!";
                count = 0;
            }
            displayTimer(str);
        }
    };


    //static int i = 0;

    /*开始计时器计时*/
    public static void startTimer(int c) {
        if ((TcpSocket.isConnecting == true) && (TcpSocket.mSocketClient != null)) {
            count = c;
            if (count == TI) {
                displayTimer("..." + TI);
            }

            timer = new Timer();
            task = new TimerTask() {
                public void run() {
                    Log.e("liyuelong", "倒计时:");
                    Message msg = Message.obtain();
                    msg.what = count;
                    mHandler.sendMessage(msg);
                }
            };
            timer.schedule(task, 1000);//计时间隔为1S
        }
    }

    /*停止计时器计时*/
    public static void stopTimer() {
        try {
            Log.e("liyuelong", "timer销毁了");
            if (timer != null) {
                timer.cancel();
            }
        } catch (Exception e) {
        }
        ;
    }

    private static void displayTimer(String str) {
        switch (BaseActivity.inWhichAc) {
            case "runa.ReadMeterActivity"://runa-读取抄表
                ReadMeterActivity.rcvbyte.setText(str);
                break;
            case "runa.ModifyNumActivity.read"://runa-读取表号
                ModifyNumActivity.rcvbyte.setText(str);
                break;
            case "runa.ModifyNumActivity.set"://runa-修改表号
                ModifyNumActivity.rcvbyte.setText(str);
                break;
            case "runa.ModifyTimeActivity"://runa-重新对时
                ModifyTimeActivity.rcvbyte.setText(str);
                break;
            case "runa.SetAttributeActivity"://runa-仪表属性
                SetAttributeActivity.rcvbyte.setText(str);
                break;
            case "runa.SetDataActivity.read"://runa-数据设置--read
                SetDataActivity.rcvbyte.setText(str);
                break;
            case "runa.SetDataActivity.set"://runa-数据设置--set
                SetDataActivity.rcvbyte.setText(str);
                break;
            case "runa.FlowActivity.read"://runa-流量修正-读取
                FlowActivity.rcvbyte.setText(str);
                break;
            case "runa.FlowActivity.set"://runa-流量修正-设置
                FlowActivity.rcvbyte.setText(str);
                break;
            case "runa.CheckActivity"://runa-检定状态-设置
                CheckActivity.rcvbyte.setText(str);
                break;
            case "othercompany.RunaModusActivity"://runa-检定状态-设置
                RunaModusActivity.rcvbyte.setText(str);
                break;
            case "othercompany.RunaModusNumActivity"://runa-modbus-修改表号
                RunaModusNumActivity.rcvbyte.setText(str);
                break;
            case "othercompany.OtherCompanyActivity"://runa-检定状态-设置
                OtherCompanyActivity.rcvbyte.setText(str);
                break;
            case "othercompany.EuropeCompanyActivity"://runa-检定状态-设置
                EuropeCompanyActivity.rcvbyte.setText(str);
                break;
            case "common.RudeActivity.readpwr"://runa-检定状态-设置
                CheckActivity.rcvbyte.setText(str);
                break;

            /***************************************瑞纳阀门展示***********************************************/
            case "runavalue.ReadMeterActivity"://runa-读取数据
                com.runachina.runavalve.ReadMeterActivity.rcvbyte.setText(str);
                break;
            case "runavalue.FactoryResetActivity"://runa-恢复出厂-设置
                com.runachina.runavalve.FactoryResetActivity.rcvbyte.setText(str);
                break;
            case "runavalue.DataResetAvtivity"://runa-数据清零-设置
                com.runachina.runavalve.DataResetAvtivity.rcvbyte.setText(str);
                break;
            case "runavalue.HardwareResetActivity"://runa-硬件复位-设置
                com.runachina.runavalve.HardwareResetActivity.rcvbyte.setText(str);
                break;
            case "runavalue.ModifyNumActivity.read"://runa-修改表号 读取
                com.runachina.runavalve.ModifyNumActivity.rcvbyte.setText(str);
                break;
            case "runavalue.ModifyNumActivity.set"://runa-修改表号-设置
                com.runachina.runavalve.ModifyNumActivity.rcvbyte.setText(str);
                break;

            /*****************************************亿林阀门展示**********************************************/
            case "yilinvalve.YiLinValveReadMeterActivity"://runa-读取表号
                com.runachina.yilinvalve.YiLinValveReadMeterActivity.rcvbyte.setText(str);
                break;
            /******************************************瑞纳温度采集器********************************************/
            case "temcollector.TemReadMeterActivity"://runa-读取表号
                com.runachina.temcollector.TemReadMeterActivity.rcvbyte.setText(str);
                break;
            case "temcollector.TemFactoryResetActivity"://runa-恢复出厂
                com.runachina.temcollector.TemFactoryResetActivity.rcvbyte.setText(str);
                break;
            case "temcollector.TemDataResetActivity"://runa-数据清零
                com.runachina.temcollector.TemDataResetActivity.rcvbyte.setText(str);
                break;
            case "temcollector.TemHardwareResetActivity"://runa-硬件复位
                com.runachina.temcollector.TemHardwareResetActivity.rcvbyte.setText(str);
                break;
            case "temcollector.TemModifyNumActivity.read"://runa-修改表号 读取
                com.runachina.temcollector.TemModifyNumActivity.rcvbyte.setText(str);
                break;
            case "temcollector.TemModifyNumActivity.set"://runa-修改表号-设置
                com.runachina.temcollector.TemModifyNumActivity.rcvbyte.setText(str);
                break;

            default:
                break;
        }
    }


    /**
     * 跳转到wifi设置页面
     */
    public void goWifiSetting() {
        Intent i = new Intent();
        if (android.os.Build.VERSION.SDK_INT >= 11) {
            //Honeycomb
            i.setClassName("com.android.settings", "com.android.settings.Settings$WifiSettingsActivity");
        } else {
            //other versions
            i.setClassName("com.android.settings", "com.android.settings.wifi.WifiSettings");
        }
        startActivity(i);
    }

    /**
     * 弹出wifi列表
     */
//    public void showWifisPop(View view) {
//
//        WifiListPop wifiListPop = new WifiListPop(this, view);
//        wifiListPop.initPop();
//        Toast.makeText(this, "弹出列表", Toast.LENGTH_SHORT).show();
//    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            Log.e("liyuelong", TAG + "onDestroy,timer销毁了");
            timer.cancel();
        }


    }

    WifiInfo wifiinfo;
    String wifiIp;

    //将获取的int转为真正的ip地址,参考的网上的，修改了下
    private String intToIp(int ip) {
        return (ip & 0xFF) + "." + ((ip >> 8) & 0xFF) + "." + ((ip >> 16) & 0xFF) + "."
                + ((ip >> 24) & 0xFF);
    }

    //每次点击读取的时候，判断wifi是否连接
    public boolean setWifi() {
        if (TcpSocket.isConnecting) {
            return true;
        } else {
            Toast.makeText(this, "wifi未连接！", Toast.LENGTH_SHORT).show();
            return false;
        }
    }


    //判断当前连接的wifi
    public boolean isWifi() {
        //RU-CL-DE2-3839
        if (getWifoSSID().contains("RU-CL")) {
            TcpSocket.isConnecting = true;
            return true;
        } else {
            TcpSocket.isConnecting = false;
            return false;
        }
    }

    //获取当前 wifi SSID
    public String getWifoSSID() {
        wifiManager = (WifiManager) this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        wifiinfo = wifiManager.getConnectionInfo();
        return wifiinfo.getSSID();
    }

    /**
     * 2  * 获取版本号
     * 3  * @return 当前应用的版本号
     * 4
     */
    public String getVersion() {
        PackageManager manager = this.getPackageManager();
        PackageInfo info = null;
        try {
            info = manager.getPackageInfo(this.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        String version = info.versionName;
        return version;
    }
    /**
     * 判断亿林输入的阀门是否符合标准
     */
}
